package org.balthie.cal.core;

import java.util.Collection;
import java.util.Deque;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeSet;

/**
 * @author：balthie@126.com
 * @createtime ： 2023年3月7日 下午1:37:55
 * @description 输出传入的文本，调试用
 * @since version 初始于版本 0.0.1-SNAPSHOT
 */
public class HelpCmd extends AbsCommand implements IControllor {

	public HelpCmd() {
		super("");
	}

	@Override
	public void execute(Deque<ICommand> deque, Deque<ICommand> undoArea) {
		println("****** ", this.getLiterally(), "您可以输入以下字符 ******");
		println(" 指令说明", " | 指令输入符（别名）");

		TreeSet<ICommand> numbs = new TreeSet<>((a, b) -> {
			return duplicatedCmd(a, b) ? -1 : Integer.valueOf(a.getLiterally()) - Integer.valueOf(b.getLiterally());
		});
		TreeSet<ICommand> mathOpts = new TreeSet<>((a, b) -> {
			return duplicatedCmd(a, b) ? -1 : a.getLiterally().compareTo(b.getLiterally());
		});
		TreeSet<ICommand> ctrlOpts = new TreeSet<>((a, b) -> {
			return duplicatedCmd(a, b) ? -1 : a.getLiterally().compareTo(b.getLiterally());
		});
		TreeSet<ICommand> others = new TreeSet<>((a, b) -> {
			return duplicatedCmd(a, b) ? -1 : a.getLiterally().compareTo(b.getLiterally());
		});
		for (Entry<IInputer, ICommand> entry : cal.commandDict.entrySet()) {
			if (CalKind.LiterallyNumb == entry.getValue().getCalKind()) {
				numbs.add(entry.getValue());
			} else if (CalKind.MathOperator == entry.getValue().getCalKind()) {
				mathOpts.add(entry.getValue());
			} else if (CalKind.Controllor == entry.getValue().getCalKind()) {
				ctrlOpts.add(entry.getValue());
			} else {
				others.add(entry.getValue());
			}
		}
		println("数字指令:");
		printCmdsHelp(numbs, cal.commandDict);
		println("数学操作符指令:");
		printCmdsHelp(mathOpts, cal.commandDict);
		println("功能指令:");
		printCmdsHelp(ctrlOpts, cal.commandDict);
		println("其它指令:");
		printCmdsHelp(others, cal.commandDict);

		println("******************************");
	}

	private boolean duplicatedCmd(ICommand a, ICommand b) {
		if (a != b && a.getLiterally().equals(b.getLiterally())) {
			// 同一个指令的不同实例化对象
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	private void printCmdsHelp(Collection<ICommand> numbs, Map<IInputer, ICommand> commandDict) {
		if (numbs == null)
			return;
		for (ICommand c : numbs) {
			print("  ", c.getTips(), " | ");
			int i = 0;
			for (Map.Entry<IInputer, ICommand> entry : commandDict.entrySet()) {
				if (entry.getValue() == c) {
					if (i++ > 0)
						print(", ");
					print("'", entry.getKey().getDisplayName().equals("") ? "回车键" : entry.getKey().getDisplayName(),
							"'");
				}
			}
			println();
		}
	}

	@Override
	public boolean isCompleted(Deque<ICommand> deque) {
		return Boolean.FALSE;
	}

	@Override
	public void isLegal(Deque<ICommand> workArea, Deque<ICommand> undoArea) {
		// echo 指令随时可以运行
		return;
	}

	@Override
	public String getLiterally() {
		return "帮助: ";
	}

	@Override
	public String getTips() {
		return "帮助：查看指令集";
	}
}
